﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UniRlv.UI.CodeLook
{
    public partial class FrmSrvSetting : DevComponents.DotNetBar.OfficeForm
    {
        public FrmSrvSetting()
        {
            InitializeComponent();
        }

        private void FrmSrvSetting_Load(object sender, EventArgs e)
        {

            if (this.Tag != null)
            {
                string[] items = this.Tag.ToString().Split(new char[] { '|'});
                ListViewItem lvi;
                foreach (var item in items)
                {
                    string[] subItems = item.Split(new char[] { ',' });

                    if (subItems.Length>1)
                    {
                        lvi = new ListViewItem(new string[] { (lvDetails.Items.Count + 1).ToString(), subItems[0], subItems[1] });

                        if (lvDetails.Items.Count%2==0)
                        {
                            lvi.BackColor = Color.Silver;
                        }
                        else
                        {
                            lvi.BackColor = Color.LightGray;
                        }
                        lvDetails.Items.Add(lvi);
                    }
                    
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem(new string[] { (lvDetails.Items.Count + 1).ToString(), txtName.Text, txtIP.Text });

            if (lvDetails.Items.Count % 2 == 0)
            {
                lvi.BackColor = Color.Silver;
            }
            else
            {
                lvi.BackColor = Color.LightGray;
            }

            lvDetails.Items.Add(lvi);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lvDetails.SelectedItems != null)
            {
                lvDetails.Items.Remove(lvDetails.SelectedItems[0]);
            }
            else
            {
                MessageBoxEx.Show("先选择需要操作的记录！");
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (lvDetails.SelectedItems != null)
            {
                int idx = lvDetails.SelectedIndices[0];

                lvDetails.Items[idx].SubItems[1].Text = txtName.Text;
                lvDetails.Items[idx].SubItems[2].Text = txtIP.Text;
            }
            else
            {
                MessageBoxEx.Show("先选择需要操作的记录！");
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            if (lvDetails.Items.Count>0)
            {
                this.Tag = string.Format("{0},{1}", lvDetails.Items[0].SubItems[1].Text, lvDetails.Items[0].SubItems[2].Text);

                for (int i = 1; i < lvDetails.Items.Count; i++)
                {
                    this.Tag += string.Format("|{0},{1}", lvDetails.Items[i].SubItems[1].Text, lvDetails.Items[i].SubItems[2].Text);
                }
            }
            else
            {
                this.Tag = "";
            }

            Close();
        }

    }
}
